package com.example.SBootDemo.controller.system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.SBootDemo.ExceptionHandel.exception.Myexception;
import com.example.SBootDemo.Util.PageInfo;
import com.example.SBootDemo.Util.TableTitles;
import com.example.SBootDemo.Util.funplace;
import com.example.SBootDemo.model.function.user;
import com.example.SBootDemo.service.Interface.function.userinterface;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;


@Controller
@RequestMapping("/user")
public class UserController {
	

	@Autowired
	userinterface uservice;
	
	//跳转添加用户
	 @RequestMapping(value="/addUser", method=RequestMethod.GET)
	 public String toUser(HttpServletRequest request) {
		 uservice.addpage(request);
	     return "/fun/update/userUpdate";
	 }
	 //添加成功后返回list页面
	 @RequestMapping(value="/addUser", method=RequestMethod.POST)
	 public String addUser(@RequestBody user user) {
	     
	     return "/getUsers";
	 }
	 //删除用户
	 @RequestMapping(value="/deleteUser", method=RequestMethod.POST)
	 @ResponseBody
	 public String deleteUser(@RequestBody List<Integer> listid) throws Myexception{
		 
	     uservice.deleteUser(listid);
	     String message = "删除成功";
	     ObjectMapper mapper = new ObjectMapper();
	   
		 try {
			return mapper.writeValueAsString(message);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	 }
	 //删除用户
	 @RequestMapping(value="/deleteUser", method=RequestMethod.GET)
	 public String deleteOneUser(int id) throws Myexception{
		 List<Integer> deleteId = new ArrayList<>();
		 deleteId.add(id);
	     uservice.deleteUser(deleteId);
		 return "redirect:/user/getusers";
	 }
	 
	 
	 //返回修改信息页面
	 @RequestMapping(value="/updateUser", method=RequestMethod.GET)
	 public String toupdateUser(int id,HttpServletRequest request) {
		 uservice.updatepage(request,id);
		 return "/fun/update/userUpdate";
	 }
	 
	 @RequestMapping(value="/updateUser", method=RequestMethod.POST)
	 public String updateUser( user user,HttpServletRequest request) throws Myexception {
		 uservice.updateUser(user,request);
	     return "redirect:/user/getusers";
	 }
	 
	 //返回userlist页面
	 @RequestMapping(value="/getusers", method=RequestMethod.GET)
	 public String getUsers(HttpServletRequest request,@RequestParam(value="pageinfo", defaultValue="0") int pageinfo) throws Myexception {
		 int flag=uservice.getUsers(pageinfo,request);
		 Subject currentUser = SecurityUtils.getSubject();
		 
		 if(currentUser.isPermitted("/user/getusers")){
			 System.out.println("*********************************"+"当前用户拥有这个权限");
		 }else{
			 System.out.println("*********************************"+"当前用户没有这个权限");
		 }
		 
	     return "/fun/listModel";
	 }
	 

	

}
